<?php


function config(){
	return[
	'webhook'=>'https://oapi.dingtalk.com/robot/send?access_token=c12d690b2c32dd0d484cd984d1fe0839b0c4fc1019861a74600fa9494f883a0c',
	'sce'=>'SEC0b9018d97df5d999a1737bc10d761c5d5cf7ec1e4693f8d119bac09722d59492'
	];
}
/**
     * 发送请求
     * @param $post_string
     * @return mixed
     */
    function request_by_curl($post_string) {
    $ch = curl_init();
    $remote_server=sign_web_hook();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }
	
	function sign_web_hook(){
        list($s1, $s2) = explode(' ', microtime());
        $timestamp = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        $data = $timestamp . "\n" . config()['sce'];
        $signStr = base64_encode(hash_hmac('sha256', $data, config()['sce'],true));
        $signStr = utf8_encode(urlencode($signStr));
        $hookUrl = config()['webhook'];
		$hookUrl .= "&timestamp=$timestamp&sign=$signStr";
        return $hookUrl;

    }
	
	function Text($content, $atMobiles = null, $isAtAll = true){
        $textString['msgtype']='text';
        $textString['text']=array(
            'content'=>$content
        );
        $textString['at']=array(
            'atMobiles'=>$atMobiles,
            'isAtAll'=>$isAtAll
        );
        return request_by_curl(json_encode($textString));

    }